package com.lanlian.cayman.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.lanlian.commonlib.dao.DBManager;
import com.lanlian.cayman.R;
import com.lanlian.utillib.DisplayUtil;

/**
 * <pre>
 *     date   : 2018/8/22 20:09
 *     desc   : 交易确认
 *     version: 1.0
 * </pre>
 */
public class TradeConfirmDialog extends Dialog implements View.OnClickListener {

    private Context mContext;

    private TextView mTvContent;
    private CheckBox mCbConfirm;
    private TextView mTvCancel;
    private TextView mTvOk;

    private String content;
    private View.OnClickListener onClickListener;

    public TradeConfirmDialog(@NonNull Context context) {
        super(context, R.style.customDialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_trade_confim);
        ViewGroup.LayoutParams params = getWindow().getAttributes();
        params.width = (int) (DisplayUtil.getScreenWidth(mContext) * 0.8);
        initView();
        setListeners();
    }

    private void initView() {
        mTvContent = findViewById(R.id.tv_content);
        mCbConfirm = findViewById(R.id.cb_confirm);
        mTvCancel = findViewById(R.id.tv_cancel);
        mTvOk = findViewById(R.id.tv_ok);
        mTvContent.setText(content);
    }

    private void setListeners() {
        mTvCancel.setOnClickListener(this);
        mTvOk.setOnClickListener(onClickListener);
        mCbConfirm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DBManager.getInstance().setSecondConfirm(!isChecked);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            default:
                break;
        }
    }

    public void setOnOk(View.OnClickListener listener) {
        this.onClickListener = listener;
    }

    public void setText(String content) {
        this.content = content;
    }

    public void showDialog() {
        show();
    }

}
